package server.communication.ClientMessages;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.concurrent.Semaphore;

import server.communication.Client;

public abstract class ClientMessageThread extends Thread{

	protected BufferedReader in;
	protected PrintWriter out;
	protected Client parent;
	protected Semaphore stopPause = new Semaphore(1);
	
	public ClientMessageThread(Client parent) throws IOException
	{
		this.parent = parent;
		in = new BufferedReader(new InputStreamReader(parent.getSocket().getInputStream()));
		out = new PrintWriter(parent.getSocket().getOutputStream(), true);
	}
	
	public void Resume()
	{
		try
		{
			if (stopPause.availablePermits() == 0)
				stopPause.release();
		}
		catch(Exception e){}
		paused = false;
		resume();
	}
	
	protected void dontPauseNow()
	{
		try
		{
			stopPause.acquire();
		}
		catch(Exception e){}
	}
	
	protected void pauseOK()
	{
		stopPause.release();
	}
	
	boolean paused = false;
	public void pause()
	{
		dontPauseNow();
		paused = true;
		this.suspend();
	}
	
	public boolean isPaused()
	{
		return paused;
	}
}
